package hamaster.jpbc.gui;

import hamaster.jpbc.Main;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class WelcomePanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 2594696924622639952L;

	private Main application;
	private TaskIcon encrypt;
	private TaskIcon decrypt;
	private TaskIcon settings;
	private TaskIcon help;

	public WelcomePanel(Main application) {
		this.application = application;
		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);
		JLabel l = new JLabel(Messages.getString("WelcomePanel.0")); //$NON-NLS-1$
		JPanel up = new JPanel(new FlowLayout(FlowLayout.LEFT));
		up.add(Box.createHorizontalStrut(20));
		up.add(l);
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		gbl.setConstraints(up, c);
		add(up);
		encrypt = new TaskIcon(new ImageIcon("res/lock.jpg"), Messages.getString("WelcomePanel.2")); //$NON-NLS-1$ //$NON-NLS-2$
		encrypt.getButton().addActionListener(this);
		c.gridx = 0;
		c.gridy = 1;
		gbl.setConstraints(encrypt, c);
		add(encrypt);
		Component lp0 = Box.createHorizontalStrut(10);
		c.gridx = 1;
		c.gridy = 1;
		gbl.setConstraints(lp0, c);
		add(lp0);
		decrypt = new TaskIcon(new ImageIcon("res/unlock.jpg"), Messages.getString("WelcomePanel.4")); //$NON-NLS-1$ //$NON-NLS-2$
		decrypt.getButton().addActionListener(this);
		c.gridx = 2;
		c.gridy = 1;
		gbl.setConstraints(decrypt, c);
		add(decrypt);
		settings = new TaskIcon(new ImageIcon("res/gear.jpg"), Messages.getString("WelcomePanel.6")); //$NON-NLS-1$ //$NON-NLS-2$
		settings.getButton().addActionListener(this);
		c.gridx = 0;
		c.gridy = 2;
		gbl.setConstraints(settings, c);
		add(settings);
		Component lp1 = Box.createHorizontalStrut(10);
		c.gridx = 1;
		c.gridy = 2;
		gbl.setConstraints(lp1, c);
		add(lp1);
		help = new TaskIcon(new ImageIcon("res/help.jpg"), Messages.getString("WelcomePanel.8")); //$NON-NLS-1$ //$NON-NLS-2$
		help.getButton().addActionListener(this);
		c.gridx = 2;
		c.gridy = 2;
		gbl.setConstraints(help, c);
		add(help);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == encrypt.getButton()) {
			application.showEncryptPanel();
			return;
		}
		if (e.getSource() == decrypt.getButton()) {
			application.showDecryptPanel();
			return;
		}
		if (e.getSource() == settings.getButton()) {
			application.showSettingsPanel();
			return;
		}
		if (e.getSource() == help.getButton()) {
			application.showHelpPanel();
		}
	}
}

class TaskIcon extends JPanel {
	private static final long serialVersionUID = -909018217300748378L;

	private JButton button;

	public TaskIcon(Icon buttonIcon, String labelText) {
		this(new JButton(buttonIcon), new JLabel(labelText), buttonIcon);
	}

	public TaskIcon(String buttonText, String labelText) {
		this(new JButton(buttonText), new JLabel(labelText), null);
	}

	private TaskIcon(JButton button, JLabel label, Icon buttonIcon) {
		setLayout(new BorderLayout());
		if (buttonIcon != null) {
			button.setSize(buttonIcon.getIconWidth(), buttonIcon.getIconHeight());
		}
		this.button = button;
		JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		btnPanel.add(button);
		JPanel lbPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		lbPanel.add(label);
		add(btnPanel, BorderLayout.CENTER);
		add(lbPanel, BorderLayout.SOUTH);
	}

	public JButton getButton() {
		return button;
	}
}
